package com.naiterui.ehp.bs.doctor.vo;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.web.multipart.MultipartFile;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;

import cn.hutool.core.util.IdcardUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 备案参数VO
 */
@ApiModel(value = "RecordParamVO", description = "备案参数VO")
public class RecordParamVO implements Serializable {

    private static final long serialVersionUID = -1257115796166451298L;

    /**
     * 医生ID
     */
    @ApiModelProperty(value = "医生ID")
    private Long doctorId;

    @ApiModelProperty(value = "医生姓名")
    private String name;

    /**
     * 身份证号码
     */
    @ApiModelProperty(value = "身份证号码")
    private String idNumber;

    /**
     * 身份证正面照片
     */
    @ApiModelProperty(value = "身份证正面照片")
    private MultipartFile idCardUp;

    /**
     * 身份证反面照片
     */
    @ApiModelProperty(value = "身份证反面照片")
    private MultipartFile idCardDown;

    /**
     * 职称证书
     */
    @ApiModelProperty(value = "职称证书")
    private MultipartFile titleCertificate;

    /**
     * 医师资格证
     */
    @ApiModelProperty(value = "医师资格证")
    private MultipartFile qualification;

    /**
     * 医师资格证编号
     */
    @ApiModelProperty(value = "医师资格证编号")
    private String quaNumber;

    /**
     * 医师资格证补充页
     */
    @ApiModelProperty(value = "医师资格证补充页")
    private MultipartFile supplyQualification;

    /**
     * 签章图片
     */
    @ApiModelProperty(value = "签章图片")
    private MultipartFile sealImg;

    /**
     * 参数校验
     */
    public void validate(boolean isAll, Integer titleId) throws BusinessException {

        if (this.doctorId == null || this.doctorId == 0) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        if (isAll) {
            if (StringUtils.isBlank(this.idNumber) && this.idCardUp == null || this.idCardDown == null || (this.qualification == null && this.supplyQualification == null)) {
                throw new BusinessException(ExceptionCodes.PARAM_ERROR);
            }
            /*if (!Arrays.asList(TitleUtil.Enum.PHYSICIAN.getId(), TitleUtil.Enum.MAIN_HOSPITAL_PHYSICIAN.getId()).contains(titleId)) {
                if (titleCertificate == null) {
                    throw new BusinessException(ExceptionCodes.DOCTOR_RECORD_INFO_NOT_COMPLETE);
                }
            }*/
        } else {
            if (this.idCardUp == null && this.idCardDown == null && this.qualification == null
                    && this.supplyQualification == null) {
                throw new BusinessException(ExceptionCodes.PARAM_ERROR);
            }
        }

        if (StringUtils.isNotBlank(this.idNumber)) {
            if (this.idNumber.length() == 18 && this.idNumber.toUpperCase().endsWith("*")) {
                this.idNumber = this.idNumber.substring(0, 17) + "X";
            }
            // 校验身份证
            if (!IdcardUtil.isValidCard(this.idNumber)) {
                throw new BusinessException(ExceptionCodes.PARAM_ERROR_ID_CARD_NUM);
            }
        }

//        if (StringUtils.isNotBlank(this.quaNumber)) {
//            this.quaNumber = this.quaNumber.replaceAll(" ", "");
//            // 判断执业资格证号是否属于范围内
//            if (this.quaNumber.length() != 24 && this.quaNumber.length() != 27) {
//                throw new BusinessException(ExceptionCodes.PARAM_ERROR_MEDICAL_NUM);
//            }
//        }

    }

    public Long getDoctorId() {
        return this.doctorId;
    }

    public void setDoctorId(Long doctorId) {
        this.doctorId = doctorId;
    }

    public String getIdNumber() {
        return this.idNumber;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }

    public MultipartFile getIdCardUp() {
        return this.idCardUp;
    }

    public void setIdCardUp(MultipartFile idCardUp) {
        this.idCardUp = idCardUp;
    }

    public MultipartFile getIdCardDown() {
        return this.idCardDown;
    }

    public void setIdCardDown(MultipartFile idCardDown) {
        this.idCardDown = idCardDown;
    }

    public MultipartFile getTitleCertificate() {
        return this.titleCertificate;
    }

    public void setTitleCertificate(MultipartFile titleCertificate) {
        this.titleCertificate = titleCertificate;
    }

    public MultipartFile getQualification() {
        return this.qualification;
    }

    public void setQualification(MultipartFile qualification) {
        this.qualification = qualification;
    }

    public MultipartFile getSupplyQualification() {
        return this.supplyQualification;
    }

    public void setSupplyQualification(MultipartFile supplyQualification) {
        this.supplyQualification = supplyQualification;
    }

    public String getQuaNumber() {
        return this.quaNumber;
    }

    public void setQuaNumber(String quaNumber) {
        this.quaNumber = quaNumber;
    }

    public MultipartFile getSealImg() {
        return this.sealImg;
    }

    public void setSealImg(MultipartFile sealImg) {
        this.sealImg = sealImg;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
